package kt.com.bilin.coin.ui.activity

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.drawable.Drawable
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.bilin.coin.ApiConstant.OSSURL
import com.bilin.coin.R
import com.bilin.coin.databinding.ItemMaterialShareBinding
import com.bilin.coin.utils.QRCode
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import jp.wasabeef.glide.transformations.RoundedCornersTransformation
import kotlinx.android.synthetic.main.activity_material_share.*
import kt.com.bilin.coin.api.API_SERVICE
import kt.com.bilin.coin.api.ApiObserver
import kt.com.bilin.coin.api.toData
import kt.com.bilin.coin.bean.InviteBean
import kt.com.bilin.coin.bean.MaterialShareBean
import kt.com.bilin.coin.common.PATH_MATERIAL_SHARE
import kt.com.bilin.coin.common.dp
import kt.com.bilin.coin.ui.adapter.BaseRecyclerAdapter
import kt.com.bilin.coin.ui.adapter.BaseViewHolder
import kt.com.bilin.coin.utils.BigDecimalUtil
import kt.com.bilin.coin.utils.GlideUtils
import kt.com.bilin.coin.utils.ImageUploadUtils
import kt.com.bilin.coin.utils.Logger

@Route(path = PATH_MATERIAL_SHARE)
class MaterialShareActivity : BaseActivity(R.layout.activity_material_share) {
    private val mMaterialShareAdapter: MaterialShareAdapter = MaterialShareAdapter()
    private val mImageUploadUtils: ImageUploadUtils by lazy { ImageUploadUtils(this, "") {} }
    private var itemWidth: Int = 0
    private lateinit var mInviteBean: InviteBean
    override fun initView() {
        itemWidth = ((resources.displayMetrics.widthPixels - 36.dp) / 2)
        recyclerView.layoutManager = StaggeredGridLayoutManager(2, RecyclerView.VERTICAL).apply { gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_NONE }
        recyclerView.adapter = mMaterialShareAdapter
    }

    override fun initData() {
        launchUI({ API_SERVICE.getInviteMsg().toData() }, ApiObserver(this) {
            if (it != null) {
                mInviteBean = it
                mMaterialShareAdapter.datas = mutableListOf<MaterialShareBean>().apply {
                    add(MaterialShareBean("${OSSURL}/upload/image_material_1.jpg", 1080, 1920))
                    add(MaterialShareBean("${OSSURL}/upload/image_material_2.png", 1242, 2208))
                    add(MaterialShareBean("${OSSURL}/upload/image_material_3.jpg", 1080, 1920))
                    add(MaterialShareBean("${OSSURL}/upload/image_material_4.jpg", 1080, 1920))
                    add(MaterialShareBean("${OSSURL}/upload/image_material_5.jpg", 1080, 1920))
                    add(MaterialShareBean("${OSSURL}/upload/image_material_6.jpg", 1080, 1920))
                    add(MaterialShareBean("${OSSURL}/upload/image_material_7.jpg", 1080, 1920))
                    add(MaterialShareBean("${OSSURL}/upload/image_material_8.jpg", 1080, 1920))
                    add(MaterialShareBean("${OSSURL}/upload/image_material_9.jpg", 1080, 1920))
                }
                mMaterialShareAdapter.notifyDataSetChanged()
            }
        })
    }

    inner class MaterialShareAdapter : BaseRecyclerAdapter<BaseViewHolder<ItemMaterialShareBinding>, MaterialShareBean>() {
        override fun onCreateViewHolder(parent: ViewGroup, p1: Int) = BaseViewHolder(ItemMaterialShareBinding.inflate(getLayoutInflate(parent.context), parent, false))
        override fun onBindViewHolder(holder: BaseViewHolder<ItemMaterialShareBinding>, bean: MaterialShareBean, position: Int) {
            holder.mViewBinding.run {
                bean.run {
                    val scaleWidth: Float = BigDecimalUtil.div(itemWidth, width, 2).toFloat()
                    val coverHeight = (height * scaleWidth).toInt()
                    val rlImageLayoutParams: ViewGroup.LayoutParams = ivImage.layoutParams
                    rlImageLayoutParams.width = itemWidth
                    rlImageLayoutParams.height = coverHeight
                    ivImage.layoutParams = rlImageLayoutParams
                    Glide.with(ivImage.context)
                            .asBitmap()
                            .load(path)
                            .override(itemWidth, coverHeight)
                            .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                            .apply(RequestOptions().transform(CenterCrop(), RoundedCornersTransformation(10.dp, 0)))
//                            .placeholder(GlideUtils.getBackgroundColor())
                            .into(object : CustomTarget<Bitmap>() {
                                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                                    ivImage.setImageBitmap(generateQrcodeBitmap(resource, mInviteBean.link))
                                }

                                override fun onLoadCleared(placeholder: Drawable?) {
                                }
                            })
                    setViewClick(position, root)
                }
            }
        }

        private fun generateQrcodeBitmap(bgQrcode: Bitmap, content: String): Bitmap {
            val srcWidth = bgQrcode.width
            val srcHeight = bgQrcode.height
            val bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888)
            val canvas = Canvas(bitmap)
            canvas.drawBitmap(bgQrcode, 0f, 0f, null)
            val width = srcWidth / 4
            val height = width
            val qrcodeTop = srcHeight - height - 52
            val qrcodeLeft = srcWidth - width - 35
            val qrBitmap = QRCode.createQRImage(content, width, height)
            canvas.drawBitmap(qrBitmap, qrcodeLeft.toFloat(), qrcodeTop.toFloat(), null)
            return bitmap
        }

        override fun subOnClickAdapter(v: View, position: Int) {
            super.subOnClickAdapter(v, position)
            mImageUploadUtils.gallery(mutableListOf<String>().apply {
                datas.forEach { add(it.path) }
            }, position)
        }
    }
}